<?php
//display quote 
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/app/salesOrderConvertUtil.js', CClientScript::POS_BEGIN);

$countQuote = count($quoteList);

$table = new CI_Table();
$tmpl = array(
    'table_open' => '<table border="0" cellpadding="2" cellspacing="1" class="" width="500">',
    'row_start' => '<tr style="vertical-align: top;">'
);
$table->set_template($tmpl);
$table->set_heading(array(
    'Date', 'Reference', 'Number', 'Status', 'Amount'
));
echo "<h2>Quote</h2>";
if ($countQuote > 0)
{
    for ($i = 0; $i < $countQuote; $i++)
    {

        $item = $quoteList[$i];
        /* @var $item Invoice */
        $paymentList = $item->getPaymentList();
        $paymentStr = '';
        if (count($paymentList) > 0)
        {
            foreach ($paymentList as $payment)
            {
                /* @var $payment RecLed */
                $paymentStr.=$payment->entryid . '<br/>';
            }
        }
        $data = array(
            $item->invoicedate,
            NULL,
            "<a href='index.php?r=invoices/update&invoice=$item->invoice'>$item->invoice</a>",
            $item->getQuoteStatusText(),
            number_format($item->amountcharge, 2),
            $paymentStr
        );
        $table->add_row($data);
    }
    echo $table->generate();
}
else
{
    echo "No quote have been created with this job number.";
}
?>

<h2>Sales Order</h2>
<?php
$soCount = count($salesOrderList);

$table->set_heading(array(
    'Date', 'Reference', 'Number', 'Status', 'Amount', 'Deposits', ''
));
$table->set_template(array(
    'table_open' => '<table border="0" cellpadding="2" cellspacing="1" class="" width="735">',
    'row_start' => '<tr style="vertical-align: top;">'
));
if ($soCount > 0)
{
    $paymentList = $item->getPaymentList();
    $paymentStr = '';
    foreach ($salesOrderList as $item)
    {
        if (count($paymentList) > 0)
        {
            foreach ($paymentList as $payment)
            {         
                //generate the table
                $payTable = new CI_Table;
                $payTable->set_template(array(
                    'table_open' => '<table border="0" cellpadding="0" cellspacing="0" class="" width="200">',
                ));
                $payTable->set_heading(array(
                    'Amount', 'Memo'
                ));
                //can apply to invoice, must have invoice to apply

                $payTable->add_row(array($payment->totalAmount, $payment->memo));
                //register the tooltip script

                Yii::app()->clientScript->registerScript("payment-$payment->refNumber", "
                                        $('.payment-$payment->refNumber').hide();
                                        $('#payment-$payment->refNumber').qtip({
                                            content:$('.payment-$payment->refNumber')
                                            });
                                        ");



                $paymentStr.=<<<HERE
                                        <a href="#" id='payment-$payment->refNumber'>$payment->totalAmount - $payment->memo</a> 
                                        <div  class='payment-$payment->refNumber'>
                                            {$payTable->generate()}
                                        </div><br/>
                                        
HERE;
            }
        }


        $data = array(
            $item->invoicedate,
            $item->quoteorg,
            "<a href='index.php?r=invoices/update&invoice=$item->invoice'>$item->invoice</a>",
            JobsStatus::getText($item->salesorderstatus),
            number_format($item->amountcharge, 2),
            $paymentStr,
                //$convertUtil->isSalesOrderConvertable($item->invoice) ? $convertLink : null
        );
        $table->add_row($data);
    }
    echo $table->generate();
}
else
{
    echo 'No sales orders have been created with this job number.';
}
?>
<br/>
<h2>Invoice</h2>
<?php
$invCount = count($invList);
$table->set_template(array(
    'table_open' => '<table border="0" cellpadding="2" cellspacing="1" class="" width="635">',
    'row_start' => '<tr style="vertical-align: top;">'
));
$table->set_heading(array(
    'Type', 'Date', 'Reference', 'Number', 'Status', 'Amount', 'Payments'
));
if ($invCount > 0)
{


    foreach ($invList as $item)
    {
        $paymentList = $item->getPaymentList();
        $paymentStr = '';

        if (count($paymentList) > 0)
        {


            foreach ($paymentList as $payment)
            {
                /* @var $payment RecLed */

                //generate the table
                $payTable = new CI_Table;
                $payTable->set_template(array(
                    'table_open' => '<table border="0" cellpadding="0" cellspacing="0" class="" width="200">',
                ));
                $payTable->set_heading(array(
                    'Amount', 'Entry Date', 'Type'
                ));
                $payTable->add_row(array($payment->invoiceAmount, $payment->date, $payment->paymentMethodRefFullName));
                //register the tooltip script
                Yii::app()->clientScript->registerScript("payment-$payment->invoiceTaxnID", "
                                        $('.payment-$payment->invoiceTaxnID').hide();
                                        $('#payment-$payment->invoiceTaxnID').qtip({
                                            content:$('.payment-$payment->invoiceTaxnID')
                                            });
                                        ");
                $paymentStr.=<<<HERE
                                        <a href="#" id='payment-$payment->invoiceTaxnID'>$payment->invoiceTaxnID - $payment->invoiceAmount</a>
                                        <div  class='payment-$payment->invoiceTaxnID'>
                                            {$payTable->generate()}
                                        </div><br/>
                                        
HERE;
            }
        }
        if ($item->invoicetype == Invoice::TYPE_CREDIT_MEMO)
        {
            $reference = $item->invoiceorg;
            $itemStatus = '';
        }
        else
        {
            $reference = $item->salesordersorg;
            $itemStatus = $item->getQuoteStatusText();
        }
        $data = array(
            $item->invoicetype,
            $item->invoicedate,
            $reference,
            "<a href='index.php?r=invoices/update&invoice=$item->invoice'>$item->invoice</a>",
            $itemStatus,
            number_format($item->amountcharge, 2),
            $paymentStr
        );
        $table->add_row($data);
    }
    echo $table->generate();
}
else
{
    echo 'No invoice have been created with this job number.';
}
?>
